package com.admin.controller;


import com.admin.service.IPermissionService;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/permission")
public class PermissionController {
    @Autowired
    private IPermissionService iPermissionService;

    /**
     * 查权限树的数据并查所有用户所有权限id
     * @param roleId
     * @return
     */
        @RequestMapping("/findPermissionByRole")
    public ResponseBean findPermissionByRole(int roleId){
            Map responseBody = iPermissionService.findPermissionByRole(roleId);
            return new ResponseBean(200,"success",responseBody);
        }

    /**
     * 保存权限树的权限编号
     * @param permissionIdArray
     * @param roleId
     * @return
     */
        @RequestMapping("/saveOrDelPermissionByRole")
    public ResponseBean saveOrDelPermissionByRole(int[] permissionIdArray,int roleId){
            int n = iPermissionService.saveOrDelPermissionByRole(permissionIdArray,roleId);
            return new ResponseBean(200,"success",null);
        }

    /**
     * 查所有权限
     * @return
     */
    @RequestMapping("/findAllPermission")
    public ResponseBean findPermission(){
        Map responseBody = iPermissionService.findPermission();
        return new ResponseBean(200,"success",responseBody);
    }
    @RequestMapping("/savePermission")
    public ResponseBean savePermission(String rolename,int[] permissionIdArray){
        int n = iPermissionService.savePermission(permissionIdArray,rolename);
        if(n>0) {
            return new ResponseBean(200, "success", null);
        }else{
            return new ResponseBean(500, "失败", null);
        }
    }
    @RequestMapping("/PermissionUpdate")
    public ResponseBean permissionUpdate(@RequestParam(defaultValue = "-1") int id, String text,String url,String end,@RequestParam(defaultValue = "-1") int pid){
        int n;
        if(pid != -1){
            n = iPermissionService.permissionInsert(url,text,end,pid);
        }else{
            n = iPermissionService.permissionUpdate(id,text,url,end);
        }
        if(n>0) {
            return new ResponseBean(200, "success", null);
        }else{
            return new ResponseBean(500,"失败",null);
        }
    }
    @RequestMapping("/PermissionFindById")
    public ResponseBean permissionUpdate(int id)
    {
        Map map = iPermissionService.findUrlById(id);
        return new ResponseBean(200,"success",map);
    }
    @RequestMapping("/PermissionDel")
    public ResponseBean permissionDel(int id)
    {
        int n = iPermissionService.delById(id);
        if(n>0) {
            return new ResponseBean(200, "success", null);
        }else{
            return new ResponseBean(500, "fail", null);
        }
    }
}
